﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using FineUI;
using mydddd.BLL.other;


namespace mydddd.Web.Demo.sysRolepopedom
{
    public partial class RoleEdit : BasePage
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                try
                {
                    mydddd.BLL.sysRolepopedom bll = new mydddd.BLL.sysRolepopedom();
                    mydddd.Model.sysRolepopedom mo = bll.GetModel(int.Parse(Request["id"]));
                    txb_name.Text = mo.rname;

                    BLL.SysModule bllModule = new BLL.SysModule();
                    CreateTree(Tree1, bllModule.GetAllList(), (IList)(mo.moduleid.Split(',')), "moduleName", "moduleIcon", "parentID", "moduleUrl");
                }
                catch (Exception)
                {
                    Response.End();
                }

                
            }

        }

        #region 绑定tree

        /// <summary>
        /// 创建tree
        /// </summary>
        /// <param name="tree">tree控件ID</param>
        /// <param name="ds">数据集</param>
        /// <param name="nodeText">node显示的字段</param>
        /// <param name="nodeIcon">node图标字段</param>
        /// <param name="nodePid">node父id字段</param>
        /// <param name="navigateUrl">链接地址</param>
        public static void CreateTree(FineUI.Tree tree, DataSet ds, IList checkValue, string nodeText, string nodeIcon, string nodePid, string navigateUrl)
        {
            DataRow[] drParent = ds.Tables[0].Select(nodePid + "=0");

            TreeNode node;
            foreach (DataRow dr in drParent)
            {
                node = addTreeNode(dr,checkValue, nodeText, nodeIcon,  navigateUrl);

                tree.Nodes.Add(node);

                ResolveSubTree(ds, checkValue,dr["id"].ToString(), node, nodeText, nodeIcon, nodePid, navigateUrl);
            }
        }

        /// <summary>
        /// 循环子节点
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <param name="parentId">父编号</param>
        /// <param name="nodeText">node显示的字段</param>
        /// <param name="nodeIcon">node图标字段</param>
        /// <param name="nodePid">node父id字段</param>
        /// <param name="navigateUrl">链接地址</param>
        private static void ResolveSubTree(DataSet ds, IList checkValue, string parentId, TreeNode treeNode, string nodeText, string nodeIcon, string nodePid, string navigateUrl)
        {
            DataRow[] drParent = ds.Tables[0].Select(nodePid + "=" + parentId + "");

            TreeNode node;
            foreach (DataRow dr in drParent)
            {
                node = addTreeNode(dr, checkValue, nodeText, nodeIcon, navigateUrl);
                treeNode.Nodes.Add(node);
                ResolveSubTree(ds,checkValue, dr["id"].ToString(), node, nodeText, nodeIcon, nodePid, navigateUrl);
            }
        }

        private static TreeNode addTreeNode(DataRow dr, IList checkValue, string nodeText, string nodeIcon, string navigateUrl)
        {
            TreeNode node = new TreeNode();
            node.NodeID = dr["id"].ToString();
            node.Text = dr[nodeText].ToString();
            if (dr[nodeIcon].ToString().Length > 0)
                node.IconUrl = "/" + dr[nodeIcon].ToString();

            node.Expanded = true;
            node.EnableCheckBox = true;
            node.EnableCheckEvent = true;


            //判断node.NodeID是否在checkValue中
            if (( checkValue).Contains(node.NodeID))
            {
                node.Checked = true;
            }

            string tempurl = dr[navigateUrl].ToString();
            if (!string.IsNullOrEmpty(tempurl))
                node.NavigateUrl = tempurl;

            return node;
        }

        #endregion


        protected void Tree1_OnNodeCheck(object sender, TreeCheckEventArgs e)
        {
            if (e.Checked)
            {
                Tree1.CheckAllNodes(e.Node.Nodes);
            }
            else
            {
                Tree1.UncheckAllNodes(e.Node.Nodes);
            }
        }

        protected void Button_add_OnClick(object sender, EventArgs e)
        {
            TreeNode[] nodes = Tree1.GetCheckedNodes();

            StringBuilder sb = new StringBuilder();
            if (nodes.Length > 0)
            {
                foreach (TreeNode node in nodes)
                {
                    sb.AppendFormat(",{0}", node.NodeID);
                }
            }
            else
            {
                AlertQuestion("请选择页面模块！", false);
                return;
            }

            mydddd.BLL.sysRolepopedom bll = new mydddd.BLL.sysRolepopedom();
            mydddd.Model.sysRolepopedom mo = new mydddd.Model.sysRolepopedom();
            mo.rname = txb_name.Text;
            mo.state = 1;
            mo.moduleid = sb.ToString().Substring(1);
            mo.id = int.Parse(Request["id"]);
            
            if (bll.Update(mo))
            {

                //设置缓存
                BLL.SysModule sysModulebll = new BLL.SysModule();
                DataSet ds = sysModulebll.GetList(" id in (" + mo.moduleid + ") order by sort ");
                CacheHelper.Insert(BLL.sysRolepopedom.CacheName() + mo.id, ds);

                AlertInfor("修改成功！", true);
            }
            else
            {
                Alert.Show("修改失败！");
            }


        }
        
    }
}